package com.totoro.transfer;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

import java.io.*;
import java.time.LocalTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class CeceT20180517 {

    public static void transfer() {

        String dir = "/Users/swy/Documents/cece/cece_asr0517/";
        String sqlFile = "sql20180517_1_200.sql";
        String batchId = "201805171438";
        //图片
        Map<String, String> cache = new HashMap();
        try {
            BufferedReader readerCvs = new BufferedReader(new FileReader(new File(dir + "data.csv")));
            String line1 = null;
            while ((line1 = readerCvs.readLine()) != null) {
                String key = line1.replaceAll("^.+/([^/]+)$", "$1");
                cache.put(key, line1);
                //cache.put(key, line1.split(",")[1]);
            }
            readerCvs.close();

            File dirFile = new File(dir + "srt/");
            File[] fileList = dirFile.listFiles();
            System.out.println(String.format("开始处理文件..."));
            BufferedWriter writer = new BufferedWriter(new FileWriter(new File(dir + sqlFile)));

            for (int i = 0; i < fileList.length; i++) {
                //跳过data数据
                if (fileList[i].getName().contains(".csv") || fileList[i].getName().contains(".DS_Store")) {
                    continue;
                }

                List<String> list = new ArrayList();
                //region json处理
                //读取文件
                StringBuffer sb = new StringBuffer();
                try {
                    BufferedReader reader = new BufferedReader(new FileReader(fileList[i]));
                    String line = null;
                    while ((line = reader.readLine()) != null) {
                        sb.append(line);
                        list.add(line);
                    }
                    reader.close();
                } catch (Exception ex) {

                }
                if (list.size() % 4 != 0) {
                    list.remove(list.size() - 1);
                }
                System.out.println("///"+fileList[i].getName()+"//"+list.size());

                //写入json
                JSONObject result = new JSONObject();
                JSONArray rects = new JSONArray();
                result.put("name", fileList[i].getName().split("-字幕-")[0]);
                LocalTime seconds = LocalTime.parse(list.get(list.size() - 3).split("-->")[1].trim().replace(",", "."));

                result.put("seconds", (seconds.getHour() * 60 * 60) + (seconds.getMinute() * 60) + seconds.getSecond() + seconds.getNano() / 1000000000.000f);
                try {
                    if (list.size() % 4 != 0) {
                        throw new Exception();
                    }
                    //插入第一行
                    LocalTime first_start_seconds = LocalTime.parse(list.get(1).split("-->")[0].trim().replace(",", "."));
                    float firstStart = (first_start_seconds.getHour() * 60 * 60) + (first_start_seconds.getMinute() * 60) + first_start_seconds.getSecond() + (first_start_seconds.getNano() / 1000000000.000f);
                    if (firstStart > 1.00f) {
                        JSONObject temp1 = new JSONObject();
                        temp1.put("start_seconds", 0.00f);
                        temp1.put("end_seconds", firstStart);
                        temp1.put("x1", (0.00f) * 70 + 20.5);
                        temp1.put("x2", (firstStart) * 70 + 20.5);

                        temp1.put("y1", 0);
                        temp1.put("y2", 200);
                        temp1.put("noise", "");
                        temp1.put("id", System.currentTimeMillis());
                        temp1.put("sex", "");
                        temp1.put("identity", "");

                        temp1.put("text", "");
                        System.out.println("首行大于一秒" + temp1.toJSONString());
                        rects.add(temp1);
                    } else {
                        System.out.println("首行小于一秒！" + 0.00f + "//" + firstStart);
                    }

                    for (int k = 0; k < list.size(); k = k + 4) {
                        //正常数据写入
                        JSONObject temp = new JSONObject();
                        LocalTime start_seconds = null;
                        //第一条处理
                        if (k == 0 && firstStart < 1.00f) {
                            start_seconds = LocalTime.parse("00:00:00.000");
                        } else {
                            start_seconds = LocalTime.parse(list.get(k + 1).split("-->")[0].trim().replace(",", "."));
                        }

                        //正常数据写入
                        float start = (start_seconds.getHour() * 60 * 60) + (start_seconds.getMinute() * 60) + start_seconds.getSecond() + (start_seconds.getNano() / 1000000000.000f);
                        temp.put("start_seconds", start);

                        LocalTime end_seconds = LocalTime.parse(list.get(k + 1).split("-->")[1].trim().replace(",", "."));
                        float end = (end_seconds.getHour() * 60 * 60) + (end_seconds.getMinute() * 60) + end_seconds.getSecond() + (end_seconds.getNano() / 1000000000.000f);
                        temp.put("end_seconds", end);
                        //System.out.println("start_seconds:" + start);
                        //System.out.println("end_seconds:" + end);

                        temp.put("x1", (start) * 70 + 20.5);
                        temp.put("x2", (end) * 70 + 20.5);

                        temp.put("y1", 0);
                        temp.put("y2", 200);
                        temp.put("noise", "");
                        temp.put("id", System.currentTimeMillis() + k);
                        temp.put("sex", "");
                        temp.put("identity", "");
                        temp.put("text", list.get(k + 2).replace("'","\\'").replace("\\\\","\\"));


                        if (k + 4 + 1 < list.size()) {
                            LocalTime next_start_seconds = LocalTime.parse(list.get(k + 4 + 1).split("-->")[0].trim().replace(",", "."));
                            float nextStart = (next_start_seconds.getHour() * 60 * 60) + (next_start_seconds.getMinute() * 60)
                                    + next_start_seconds.getSecond() + (next_start_seconds.getNano() / 1000000000.000f);
                            if (nextStart - end >= 0.00f && nextStart - end < 1.0f) {
                                temp.put("end_seconds", nextStart);
                                temp.put("x2", (nextStart) * 70 + 20.5);

                                System.out.println("我们小于一秒！" + end + "//" + nextStart);
                            }
                            rects.add(temp);
                            if (nextStart - end > 1.0f) {
                                JSONObject temp1 = new JSONObject();
                                temp1.put("start_seconds", end);
                                temp1.put("end_seconds", nextStart);
                                temp1.put("x1", (end) * 70 + 20.5);
                                temp1.put("x2", (nextStart) * 70 + 20.5);

                                temp1.put("y1", 0);
                                temp1.put("y2", 200);
                                temp1.put("noise", "");
                                temp1.put("id", System.currentTimeMillis() + k);
                                temp1.put("sex", "");
                                temp1.put("identity", "");

                                temp1.put("text", "");
                                System.out.println("强势插入" + temp1.toJSONString());
                                rects.add(temp1);
                            }
                        } else {
                            rects.add(temp);
                        }


                    }
                    result.put("rects", rects);

                } catch (Exception ex) {
                    ex.printStackTrace();
                }
                //endregion

                //region SQL生成
                String sql = String.format(
                        "%s VALUES (558,'" + batchId + "','{\"image\":\"%s\"}',1,'{}',0,0,1,0,0,0,'{\"markData\":%s}',null);\n",
                        "INSERT INTO `tags_task_libs` (`tid`, `batch_id`, `question`, `type`, `properties`, `ctime`, `uid`, `status`, `lock`, `lock_status`, `lock_time`, `answer`, `log_id`)",
                        cache.get(fileList[i].getName().split("-字幕-")[0]),
                        result.toJSONString()
                );

                System.out.println(sql);
                writer.write(sql);

                //endregion
            }
            writer.flush();
            writer.close();
        } catch (Exception ex) {
            ex.printStackTrace();
        }

    }

    public static void main(String[] args) {
        transfer();
    }
}
